﻿using BepInEx;
using UnityEngine;
using FMOD.Studio;
using System;
using HarmonyLib;

namespace hatbox_hack
{
    public static class GameObjects
    {
        public static Plugin hatboxHack;
    }

    [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
    public class Plugin : BaseUnityPlugin
    {
        RealCapture realCapture;
        CheatUIMode[] modes;
        int cnt = 0;
        bool cheat_visible = false;
        bool p_pressing = false;

        private void Awake()
        {
            Logger.LogInfo($"{PluginInfo.PLUGIN_GUID} 加载喽~");
            GameObjects.hatboxHack = this;
            this.realCapture = new RealCapture();
            this.modes = (CheatUIMode[])Enum.GetValues(typeof(CheatUIMode));

            // 子模块 合约
            new Contract();

            new Harmony(PluginInfo.PLUGIN_GUID).PatchAll();
        }

        public void log(string content)
        {
            Logger.LogInfo(content);
        }

        public void Update()
        {
            this.realCapture.capture(); 
            this.cheat_visible = (this.realCapture.uis.Length > 0);
            if (this.p_pressing&&this.cheat_visible!=this.p_pressing) {
                GameCheatUI.I.Activate(this.modes[cnt]);
            }
            if (Input.GetKeyDown(KeyCode.P))
            {
                this.p_pressing = ! this.p_pressing;             
                if (!this.cheat_visible)
                {
                    GameCheatUI.I.Activate(this.modes[cnt]);
                }
                else
                {
                    GameCheatUI.I.gameObject.SetActive(false);
                }

                AudioMgr.I.PlayEvent("event:/ui/mission_tgt_success");
            }
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                if (this.cnt <= 0)
                    cnt = 12;
                else
                    this.cnt--;
                GameCheatUI.I.Activate(this.modes[cnt]);
                AudioMgr.I.PlayEvent("event:/ui/mission_tgt_success");
            }
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                if (this.cnt >= 12)
                    cnt = 0;
                else
                    cnt++;
                GameCheatUI.I.Activate(this.modes[cnt]);
                AudioMgr.I.PlayEvent("event:/ui/mission_tgt_success");
            }
            if (Input.GetKeyDown(KeyCode.T))
            {
                this.log(ActorExtras.I.resistance.Count.ToString());
            }
            if (Input.GetKeyDown(KeyCode.Y))
            {
                Contract.I.Test();
            }
        }
    }

    public static class GameObjectsCapture
    {
        public static void printAll<T>(T[] allObjects)
            where T : MonoBehaviour
        {
            foreach (T obj in allObjects)
            {
                GameObjects.hatboxHack.log(typeof(T).Name + " : " + obj.gameObject.name);
            }
        }

        public static void printAll<T>(T[] allObjects, string name)
            where T : MonoBehaviour
        {
            foreach (T obj in allObjects)
            {
                GameObjects.hatboxHack.log(name + " : " + obj.gameObject.name);
            }
        }
    }

    public class RealCapture
    {
        public BaseCheatMgr[] mgrs;
        public GameCheatUI[] uis;

        public void capture()
        {
            this.mgrs = GameObject.FindObjectsOfType<BaseCheatMgr>();
            this.uis = GameObject.FindObjectsOfType<GameCheatUI>();
        }
    }
}
